/*初始化datagrid*/
$(function () {
    /*抽取常用组件*/
    var emp_list = $("#emp_list");
    var emp_dialog = $("#emp_dialog");
    var emp_form = $("#emp_form");
    /*对象管理方法*/
    var object = {
        add: function () {
            emp_dialog.dialog('setTitle', '添加员工');
            emp_form.form('clear');
            emp_dialog.dialog('open');
        },
        edit: function () {
            var row = emp_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
            } else {
                emp_form.form('clear');
                row['dept.id'] = row.dept ? row.dept.id : '';
                $("#emp_password").hide();
                emp_form.form('load', row);
                /*回显角色信息*/
                var ids = $.map(row.roles, function (item) {
                    return item.id;
                })
                $("#emp_roles").combobox('setValues', ids);
                emp_dialog.dialog('setTitle', '编辑员工');
                emp_dialog.dialog('open');
            }
        },
        save: function () {
            emp_form.form('submit', {
                    url: '/employee/saveOrUpdate.do',
                    onSubmit: function (param) {
                        var ids = $("#emp_roles").combobox('getValues');
                        for (i = 0; i < ids.length; i++) {
                            param['roles[' + i + '].id'] = ids[i];
                        }
                    },
                    success: function (data) {
                        var res = $.parseJSON(data);
                        if (res.success) {
                            $.messager.alert('温馨提示', res.msg, 'info', function () {
                                emp_dialog.dialog('close');
                                emp_list.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', res.msg, 'error');
                        }
                    }
                }
            );
        },
        cancel: function () {
            emp_dialog.dialog('close');
        }
        ,
        reload: function () {
            emp_list.datagrid('reload');
        }
        ,
        changeState: function () {
            var row = emp_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                return;
            }
            $.get('/employee/changeState.do', {state: !row.state, id: row.id}, function (data) {
                if (data.success) {
                    $.messager.alert('温馨提示', data.msg, 'info', function () {
                        emp_dialog.dialog('close');
                        emp_list.datagrid('reload');
                    });
                } else {
                    $.messager.alert('温馨提示', data.msg, 'error');
                }
            })
        }
        ,
        query: function () {
            var keyword = $("#keyword").textbox('getValue');
            var deptId = $("#deptId").combobox('getValue');
            var minTime = $("#minTime").datebox('getValue');
            var maxTime = $("#maxTime").datebox('getValue');

            emp_list.datagrid('load', {
                keyword: keyword,
                deptId: deptId,
                minTime: minTime,
                maxTime: maxTime
            });
        },
        exportEmp: function () {
            var keyword = $("#keyword").textbox('getValue');
            var deptId = $("#deptId").combobox('getValue');
            var minTime = $("#minTime").datebox('getValue');
            var maxTime = $("#maxTime").datebox('getValue');

            window.location.href = '/employee/export.do?keyword=' + keyword + '&deptId=' + deptId
                + '&minTime=' + minTime + '&maxTime=' + maxTime;
        },
        exportTemplate: function () {
            window.location.href = '/employee/exportTemplate.do';
        },
        importEmp: function () {
            $("#emp_import").dialog('open');
        },
        upload: function () {
            $("#emp_importForm").form('submit', {
                url: '/employee/import.do',
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', data.msg, 'info', function () {
                            $("#emp_import").dialog('close');
                            emp_list.datagrid('reload');
                        })
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        }
    }
    /*事件统一绑定*/
    $("[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        object[cmd]();
    })

    emp_list.datagrid({
        fit: true,
        rownumbers: true,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#emp_toolbar',
        url: '/employee/query.do',
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_state").linkbutton({
                    text: '离职'
                })
            } else {
                $("#btn_state").linkbutton({
                    text: '复职'
                })
            }
        },
        columns: [[
            {field: 'username', title: '用户名', width: 100},
            {field: 'realname', title: '真实姓名', width: 100},
            {field: 'tel', title: '电话', width: 100},
            {field: 'email', title: '邮箱', width: 200},
            {
                field: 'dept', title: '部门', width: 100, formatter: function (value, row, index) {
                return value ? value.name : '';
            }
            },
            {field: 'inputTime', title: '入职时间', width: 200},
            {
                field: 'state', title: '员工状态', formatter: function (value, row, index) {
                return value ? '在职' : '<font color="red">离职</font>';
            }
            }, {
                field: 'roleNames', title: '角色列表', width: 200
            },
            {
                field: 'admin', title: '管理员', formatter: function (value, row, index) {
                return value ? '<font color="green">是</font>' : '否';
            }
            }

        ]]
    })

    emp_dialog.dialog({
        closed: true,
        width: 300,
        height: 400,
        buttons: $("#emp_diaButtons"),
    })

    $("#emp_import").dialog({
        height: 150,
        width: 300,
        buttons: $("#import_button"),
        title: '选择文件',
        closed: true
    })

})
